﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Policlinica;
using System.Data;
using System.Data.SqlClient;
using NLog;

namespace ADONetDal
{
    public class ADONetRepositorySchedule : ADONetRepositoryBase<Schedule>
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();

        protected override SqlCommand GetInsertCommand(Schedule entity)
        {
            var cmd = new SqlCommand("ScheduleInsert", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("TimeFrom", entity.TimeFrom));
            cmd.Parameters.Add(new SqlParameter("TimeTo", entity.TimeTo));
            cmd.Parameters.Add(new SqlParameter("Days", entity.Days));
            logger.Trace("SqlCommand для записи данных Расписания в базу сформирован");
            return cmd;
        }

        protected override SqlCommand GetDeleteCommand(int id)
        {
            var cmd = new SqlCommand("ScheduleDelete", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Id", id));
            logger.Trace("SqlCommand для логического удаления Расписания id = {0} сформирован", id);
            return cmd;
        }

        protected override SqlCommand GetUpdateCommand(Schedule entity)
        {
            var cmd = new SqlCommand("ScheduleUpdate", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Id", entity.Id));
            cmd.Parameters.Add(new SqlParameter("TimeFrom", entity.TimeFrom));
            cmd.Parameters.Add(new SqlParameter("TimeTo", entity.TimeTo));
            cmd.Parameters.Add(new SqlParameter("Days", entity.Days));
            logger.Trace("SqlCommand для обновления данных Расписания id = {0} в базе сформирован", entity.Id);
            return cmd;
        }

        protected override SqlCommand GetSelectCommand(int id)
        {
            var cmd = new SqlCommand("GetSchedule", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Id", id));
            logger.Trace("SqlCommand для поиска Расписания id = {0} сформирован", id);
            return cmd;
        }

        protected override SqlCommand GetSelectAllCommand()
        {
            throw new NotImplementedException();
        }

        protected override SqlCommand GetSelectByParameterCommand(object parameter, string columnName)
        {
            var cmd = new SqlCommand("GetScheduleByParameter", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Parameter", parameter));
            cmd.Parameters.Add(new SqlParameter("Column", columnName));
            logger.Trace("SqlCommand для поиска Расписания {0} = {1} сформирован", columnName, parameter);
            return cmd;
        }

        protected override Schedule GetEntity(SqlDataReader sqlDataReader)
        {
            Schedule schedule = new Schedule();
            schedule.Id = (int)sqlDataReader["Id"];
            schedule.TimeFrom = (TimeSpan)sqlDataReader["TimeFrom"];
            schedule.TimeTo = (TimeSpan)sqlDataReader["TimeTo"];
            schedule.Days = sqlDataReader["Days"].ToString();
            return schedule;
        }
    }
}
